import { IssuesRemind } from '../Background/issuesRemind';
import * as $ from 'jquery';
import { SSLib } from '@lib/SSLib';

const btnTest = document.getElementById('btnTest');

btnTest.addEventListener('click', () => {
    alert(1111);
});

let btnChangeColor = document.getElementById('btnChangeColor');

chrome.storage.sync.get('color', (data) => {
    btnChangeColor.style.backgroundColor = data.color;
    btnChangeColor.setAttribute('value', data.color);
});

btnChangeColor.onclick = (element) => {
    let color = (element.target as any).value;
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        chrome.tabs.executeScript(
            tabs[0].id,
            { code: 'document.body.style.backgroundColor = "' + color + '";' });
    });
};

let btnCreateTab = document.getElementById('btnCreateTab');
btnCreateTab.onclick = (element) => {
    chrome.tabs.create({ url: 'https://www.baidu.com/' }, () => {
        alert(1111);
    });
};

document.getElementById('btnShowMedia').onclick = () => {
    let url = chrome.runtime.getURL('MediaView.html');
    chrome.tabs.create({ url }, () => { });
};

document.getElementById('btnMsg').onclick = () => {
    sendMSg('content.getVideoSrc', (data) => {
        console.log(data);
    });
};

document.getElementById('btnViewMsg').onclick = () => {

    chrome.notifications.create("slayer-issues-remind" + Math.random(), {
        type: "basic",
        iconUrl: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
        title: "提示框" + Math.random(),
        message: "23423432" + Math.random()
    });
};

function sendMSg(...arguss: any[]) {
    let argus = arguments;
    let cmdType = argus[0];
    let sendArgus = [];
    for (let i = 1; i < argus.length - 1; i++) {
        sendArgus.push(argus[i]);
    }
    let callback = argus[argus.length - 1];

    let msgObj = {
        argus: sendArgus,
        cmdType,
    };

    let queryTabs: any = { active: true, currentWindow: true };

    if (cmdType.indexOf('view.') === 0) {
        let url = chrome.runtime.getURL('MediaView.html');
        queryTabs = { url };
    }

    chrome.tabs.query(queryTabs, (tabs) => {
        chrome.tabs.sendMessage(tabs[0].id, msgObj, (response) => {
            callback(response);
        });
    });
}

setTimeout(() => {
    drawIssues();
}, 500);

function drawIssues(){
    let $div = $('#div-remind-issues');

    IssuesRemind.GetRemindIssues(data => {
        let nowDate = SSLib.dateFormatter(new Date(), 'yyyy-MM-dd');
        let $doms = data.map(item => {
            console.log(item);

            let $link = $("<a/>", {
                class: "issues-link",
                href: item.web_url,
                target: "_blank"
            });

            $link.text(item.title);
            $link.append("<span class='issues-link-date'>due_date:" + item.due_date + "</span>");

            if (item.due_date <= nowDate) {
                $link.addClass('dange-date');
            }

            return $link;
        });

        $div.append($doms);

        if ($doms.length > 0) {
            chrome.browserAction.setBadgeText({ text: "" + $doms.length })
        } else {
            chrome.browserAction.setBadgeText({ text: "" })
        }
    }, 1);
}
